package com.his.login.controller;

import com.his.login.service.LoginService;
import com.his.login.vo.LoginVo;
import com.his.login.vo.UpdatePasswordVo;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "/api/login")
public class LoginController extends ResultBaseController {

    private  final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());
    @Resource
    private LoginService loginService;

    @ResponseBody
    @PostMapping(value="/do_login.do", produces = "application/json;charset=UTF-8" )
    public ResultUtil login(@RequestBody @Validated LoginVo loginVo){
        try{
            String token = loginService.doLogin(loginVo);
            if(null != token && "" != token){
                return this.success("登录成功！",token);
            }else{
                return this.fail("用户名或密码不正确！",token);
            }
        }catch(Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @ResponseBody
    @PostMapping(value="/is_login.do")
    public ResultUtil isLogin(@RequestHeader(name="token")String token){
        try{
            String data = loginService.isLogin(token);
            return this.success("成功！",data);
        }catch(Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @ResponseBody
    @PostMapping(value="/update_password.do")
    public ResultUtil updatePassword(@RequestBody @Validated UpdatePasswordVo updatePasswordVo, @RequestHeader(name="token") String token){
        try{
            loginService.updatePassword(updatePasswordVo,token);
            return this.success("修改成功,请重新登陆！",null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @ResponseBody
    @PostMapping(value = "/logout.do")
    public ResultUtil loginOut(@RequestHeader(name="token") String token){
        try{
            loginService.loginOut(token);
            return this.success("退出成功！",null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

}
